package io.github.project_travel_mate.travel;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;


import butterknife.BindView;
import butterknife.ButterKnife;
import io.github.project_travel_mate.R;
import objects.QaItem;
import io.github.project_travel_mate.destinations.description.QaAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.app.AlertDialog;

public class AiFragment extends Fragment {


    @BindView(R.id.ai_options_recycle_view)
    RecyclerView mRecyclerView;
    
    @BindView(R.id.btn_ask_ai)
    Button mBtnAsk;
    
    @BindView(R.id.et_result)  
    EditText mEtResult;

    public AiFragment() {
    }

    public static AiFragment newInstance() {
        AiFragment fragment = new AiFragment();
        return fragment;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_ai, container, false);
        ButterKnife.bind(this, view);

        List<QaItem> qaList = new ArrayList<>();
        qaList.add(new QaItem("Q: 如何预订酒店？",
                "A: 进入 HotelsActivity，选择城市、日期后即可下单。"));
        qaList.add(new QaItem("Q: 怎样实时查看当前位置？",
                "A: 点击“实时定位”即可打开 MapViewRealTimeActivity。"));
        qaList.add(new QaItem("Q: 是否支持在线购物？",
                "A: 支持，ShoppingCurrentCityActivity 页面可直接下单。"));

        QaAdapter adapter = new QaAdapter(getActivity(), qaList);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(adapter);

        // 按钮点击弹出对话框
        mBtnAsk.setOnClickListener(v -> showQuestionDialog());
        return view;
    }
    private void showQuestionDialog() {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View dialogView  = inflater.inflate(R.layout.dialog_question, null);
        EditText etInput = dialogView.findViewById(R.id.edit_question);

        new AlertDialog.Builder(getActivity())
            .setTitle(getString(R.string.ask_ai))
            .setView(dialogView)
            .setPositiveButton("OK", (dialog, which) -> {
                String question = etInput.getText().toString().trim();
                mEtResult.setText(question);
                // TODO: 把 question 传给 AI 接口
            })
            .setNegativeButton("Cancel", null)
            .show();
    }
}
